package com.yuand.controller;

import com.yuand.config.Result;
import com.yuand.entity.Menu;
import com.yuand.mapper.MenuMapper;
import com.yuand.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yuandong
 * @date 2021/9/15 15:00
 * 菜单Controller
 */
@RestController
@RequestMapping("/sys")
public class SysMenuController {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 保存菜单
     * @param menu
     * @return
     */
    @PostMapping("/saveMenu")
    public Result saveMenu(@RequestBody Menu menu){
        menu.setName("log");
        this.menuMapper.insert(menu);
        return Result.success();
    }


    /**
     * 删除菜单
     * 1：当存在子菜单时，不能删除
     * 2：当该菜单已分配,不允许删除(暂时未做)
     * @param menuId
     * @return
     */
    @DeleteMapping("/delMenu/{menuId}")
    public Result delMenu(@PathVariable("menuId") Integer menuId){
        if(sysMenuService.hasChildren(menuId)){
            return Result.error("-1","存在子菜单时，不能删除");
        }
        this.menuMapper.deleteById(menuId);
        return Result.success();
    }
}
